<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\db\Where;
use think\facade\Env;
use think\facade\Session;
use app\api\controller\WxBizDataCrypt;//给手机号解密
// use app\admin\Model\Gkdata;
class Home extends Controller
{
	public function phone(){
	    $request=new Request();
        $sessionKey=$request->param("session_key");
        $iv=$request->param("iv");
        $encryptedData=$request->param("encryptedData");
		$appid = 'wx7cff958a038c1630';
        // $sessionKey = $session_key;
        // $encryptedData=$encryptedData;
        // $iv = ;
        $pc = new WXBizDataCrypt($appid, $sessionKey);
        $errCode = $pc->decryptData($encryptedData, $iv, $data );
        if ($errCode == 0) {
            print($data . "\n");exit;
        } else {
            print($errCode . "\n");exit;
        }

	}
	// 获取二维码
    public function getwxcode(){
        $appid="wx93ffe965c0192309";
        $secret="96018ff7a2bb9fc0e5568597b91645c7";
        $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        //缓存access_token
         Session::set('access_token','');
         Session::set('expires_in',0);
         $ACCESS_TOKEN ="";
         if(empty(Session::get('access_token')) || (!empty(Session::get('expires_in')) && time() >Session::get('expires_in')))
         {
         
             $json = $this->httpRequest($access_token );
             $json = json_decode($json,true);
             // var_dump($json);
             Session::set('access_token',$json['access_token']);
             Session::set('expires_in',time()+7200);
             $ACCESS_TOKEN =$json["access_token"];
         }
         else{
         
             $ACCESS_TOKEN = Session::get('access_token');
         }
         
        //构建请求二维码参数
        //path是扫描二维码跳转的小程序路径，可以带参数?id=xxx
        //width是二维码宽度
        // $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
        $qcode ="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$ACCESS_TOKEN";
        // "scene"=>'?goods_id='.$goods_id.'&store_id='.$store_id,
        $param = json_encode(array("path"=>"pages/index/index","width"=> 100));
        $data = [
            'scene'=>'?goods_id=1&store_id=2',
            'width'=>80,
            'page'=>'pages/index/index',
            'auto_color'=>false,
        ];
        $data = json_encode($data); 
        
        //POST参数
        $result =$this->httpRequest($qcode,$data,"POST");
        //生成二维码
        file_put_contents("./style/images/wxcode/qrcode1.png",$result);
        $base64_image ="data:image/jpeg;base64,".base64_encode($result );
        exit($base64_image);
       
        
    }
    public function httpRequest($url,$data='',$method='GET'){
            $curl = curl_init(); 
            curl_setopt($curl, CURLOPT_URL,$url); 
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 
            curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); 
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
            curl_setopt($curl, CURLOPT_AUTOREFERER, 1); 
            if($method=='POST')
            {
                curl_setopt($curl, CURLOPT_POST, 1);
                if ($data !='')
                {
                    curl_setopt($curl, CURLOPT_POSTFIELDS,$data); 
                }
            }
         
            curl_setopt($curl, CURLOPT_TIMEOUT, 30); 
            curl_setopt($curl, CURLOPT_HEADER, 0); 
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
            $result = curl_exec($curl); 
            curl_close($curl); 
            return $result;
    }
}